package csu.web.mypetstore.pojo;


import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import csu.web.mypetstore.domain.CartItem;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 *
 * @TableName lineitem
 */
@TableName(value ="lineitem")
@Data
public class LineItem implements Serializable {

    private String orderid;
    private String linenum;
    private String itemid;
    private String quantity;
    private String unitprice;
    @TableField(exist = false)
    private Item item;
    @TableField(exist = false)
    private BigDecimal total;
    public LineItem() {
    }
    public LineItem(int lineNumber, CartItem cartItem) {
        this.linenum =  String.valueOf(lineNumber);
        this.quantity =  String.valueOf(cartItem.getQuantity());
        this.itemid = cartItem.getItem().getItemid();
        this.unitprice =  String.valueOf(cartItem.getItem().getListprice());
        this.item = cartItem.getItem();
        this.total=cartItem.getTotal();
    }

    public void setItem(Item item) {
        this.item = item;
        calculateTotal();
    }

    public void calculateTotal() {
        if (item != null && item.getListprice() != null) {
            total =   BigDecimal.valueOf(item.getListprice()).multiply(new BigDecimal(quantity));
        } else {
            total = null;
        }
    }


    @TableField(exist = false)
    private static final long serialVersionUID = 1L;


    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", orderid=").append(orderid);
        sb.append(", linenum=").append(linenum);
        sb.append(", itemid=").append(itemid);
        sb.append(", quantity=").append(quantity);
        sb.append(", unitprice=").append(unitprice);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }

    public String getOrderid() {
        return orderid;
    }

    public void setOrderid(String orderid) {
        this.orderid = orderid;
    }

    public String getLinenum() {
        return linenum;
    }

    public void setLinenum(String linenum) {
        this.linenum = linenum;
    }

    public String getItemid() {
        return itemid;
    }

    public void setItemid(String itemid) {
        this.itemid = itemid;
    }

    public String getQuantity() {
        return quantity;
    }

    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }

    public String getUnitprice() {
        return unitprice;
    }

    public void setUnitprice(String unitprice) {
        this.unitprice = unitprice;
    }

    public Item getItem() {
        return item;
    }

    public BigDecimal getTotal() {
        return total;
    }

    public void setTotal(BigDecimal total) {
        this.total = total;
    }
}